package com.yxuan.springboot.service.impl;

import com.yxuan.springboot.mapper.NoticRecordMapper;
import com.yxuan.springboot.mapper.NoticTemplateMapper;
import com.yxuan.springboot.model.NoticRecord;
import com.yxuan.springboot.model.NoticTemplate;
import com.yxuan.springboot.service.NoticRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 
 * @author zchao
 *    NoticRecord业务层实现类
 * @date 2023-06-01 14:59:33
 */
@Service
@Transactional
public class NoticRecordServiceImpl extends BaseServiceImpl<NoticRecord> implements NoticRecordService {

    @Autowired
    private NoticRecordMapper noticRecordMapper;

    @Autowired
    private NoticTemplateMapper noticTemplateMapper;
    @Override
    public HashMap<String, Object> record(Map map) throws Exception {
        HashMap<String, Object> result = new HashMap<>();
        Integer id = (Integer) map.get("id");
        NoticTemplate noticTemplate = noticTemplateMapper.queryObjectById(String.valueOf(id));
        NoticRecord noticRecord = new NoticRecord();
        noticRecord.setTitle(noticTemplate.getTitle());
        noticRecord.setPicAddress(noticTemplate.getPicAddress());
        noticRecord.setSender(noticTemplate.getSender());
        noticRecord.setSendContent(noticTemplate.getSendContent());
        noticRecord.setUserId((String) map.get("userid"));
        Date date = new Date();
        Timestamp timestamp = new Timestamp(date.getTime());
        noticRecord.setSendTime(timestamp);
        noticRecord.setStatus("0");
        noticRecord.setLogicDeletion("0");
        noticRecordMapper.insert(noticRecord);
        result.put("send","success");
        return result;
    }

    @Override
    public HashMap<String, Object> allRecord(Map map) throws Exception {
        int page = (int) map.get("page");
        int limit = (int) map.get("limit");
        NoticRecord noticRecord = new NoticRecord();
        HashMap<String, Object> listByPramas = (HashMap<String, Object>) findListByPramas(page, limit, noticRecord);
        return listByPramas;
    }

    @Override
    public HashMap<String, Object> recordById(Map map) throws Exception {
        String id = (String) map.get("id");
        int page = (int) map.get("page");
        int limit = (int) map.get("limit");
        NoticRecord noticRecord = new NoticRecord();
        noticRecord.setUserId(id);
        noticRecord.setLogicDeletion("0");
        HashMap<String, Object> listByPramas = (HashMap<String, Object>) findListByPramas(page, limit, noticRecord);
        return listByPramas;
    }

    @Override
    public HashMap<String, Object> readRecord(Map map) throws Exception {
        HashMap<String, Object> result = new HashMap<>();
        int id = (int) map.get("id");
        NoticRecord noticRecord = noticRecordMapper.queryObjectById(String.valueOf(id));
        noticRecord.setStatus("1");
        Date date = new Date();
        Timestamp timestamp = new Timestamp(date.getTime());
        noticRecord.setReadTime(timestamp);
        noticRecordMapper.update(noticRecord);
        result.put("read","success");
        return result;
    }

    @Override
    public HashMap<String, Object> logicDelRecord(Map map) throws Exception {
        HashMap<String, Object> result = new HashMap<>();
        int id = (int) map.get("id");
        NoticRecord noticRecord = noticRecordMapper.queryObjectById(String.valueOf(id));
        noticRecord.setLogicDeletion("1");
        noticRecordMapper.update(noticRecord);
        result.put("logicdel","success");
        return result;
    }

    @Override
    public HashMap<String, Object> unreadNum(Map map) {
        HashMap<String, Object> result = new HashMap<>();
        String userId = String.valueOf(map.get("user_id"));
        int i = noticRecordMapper.unreadNum(userId);
        result.put("unreadNum",i);
        return result;
    }
}
